package com.dao.impl;

import com.dao.MessageDao;
import com.entity.Message;
import com.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 羡羡
 */
public class MessageDaoimpl implements MessageDao {
    QueryRunner runner=new QueryRunner();

    @Override
    public int addMessage(Message me) throws SQLException {
        String sql="insert into message(name,content,head,time) value(?,?,?,?);";
        Connection conn=DBUtil.openconn();
        int adin=runner.update(conn,sql,me.getName(),me.getContent(),me.getHead(),me.getTime());
        conn.close();
        return adin;
    }

    /**
     * 查询留言
     * @return
     * @throws SQLException
     */
    @Override
    public List search() throws SQLException {
        String sea="select * from message order by time desc";
        Connection conn =DBUtil.openconn();
        List list=runner.query(conn,sea,new BeanListHandler<Message>(Message.class));
        conn.close();
        return list;
    }

    /**
     * 删除留言
     * @param id 需要删除的id
     * @return  返回 0 or 1
     * @throws SQLException
     */
    @Override
    public int deleteMessage(int id) throws SQLException {
        String de="delete from message where id=?";
        Connection conn=DBUtil.openconn();
        int dein=runner.update(conn,de,id);
        conn.close();
        return dein;
    }
}
